package common.misc;

import java.io.UnsupportedEncodingException;

public final class StringMisc {

    public static boolean isAvailable( String source ) {

        return ( source != null && source.length() > 0 );

    } // end method

    public static String makeField( String source ) {

        if ( ! isAvailable( source ) )
            return "''";

        return " '" + source.replaceAll( "'", "''" ) + "' ";

    } // end method

    public static String fixNull( String source ) {

        if ( isAvailable( source ) )
            return source;

        return "";

    } // end method

    public static String fixNull( String source, String replace ) {

        if ( isAvailable( source ) )
            return source;

        return replace;

    } // end method

    public static String uniToEuc( String source ) {

        if ( ! isAvailable( source ) )
            return source;

        try {

            return new String( source.getBytes( "8859_1" ), "EUC-KR" );

        } catch ( UnsupportedEncodingException uee ) {

            return source;

        } // end try~catch

    } // end method

    public static String eucToUni( String source ) {

        if ( ! isAvailable( source ) )
            return source;

        try {

            return new String( source.getBytes( "EUC-KR" ), "8859_1" );

        } catch ( UnsupportedEncodingException uee ) {

            return source;

        } // end try~catch

    } // end method

    public static String makeComma( String source ) {

		String target = "";
		int length = source.length();
		int cnt = length / 3;
		int mod = length %3;
		int cnt2 = length - cnt * 3;
		
		target += source.substring( 0 , cnt2 );

		for (int i=0; i < cnt; i++){
			if ( !(i == 0 && mod == 0) )
				target += ",";			
			target += source.substring( (i*3)+cnt2 ,(i*3)+cnt2 + 3 );
		}
		return target;	
    } // end method

} // end class